<template>
  <div class="product-list">
    <h1>商品列表</h1>
    
    <!-- 搜索栏 -->
    <div class="search-bar">
      <input type="text" v-model="searchKeyword" placeholder="搜索商品...">
      <button @click="searchProducts">搜索</button>
    </div>
    
    <!-- 商品网格 -->
    <div class="products-grid">
      <div class="product-card" v-for="product in products" :key="product.id" @click="showProductDetail(product.id)">
        <img :src="product.images.split(',')[0]" :alt="product.name" v-if="product.images">
        <div v-else class="no-image">无图片</div>
        <h3>{{ product.name }}</h3>
        <p class="price">¥{{ product.price }}</p>
        <p class="description">{{ product.description ? product.description.substring(0, 50) : '' }}...</p>
        <div class="status" :class="{ 'on-sale': product.status === 1, 'off-sale': product.status === 0 }">{{ product.status === 1 ? '热卖中' : '已下架' }}</div>
      </div>
    </div>
    
    <!-- 商品详情弹窗 -->
    <div class="detail-dialog" v-if="showDetail">
      <div class="dialog-content">
        <button class="close-btn" @click="showDetail = false">&times;</button>
        
        <div v-if="selectedProduct">
          <div class="product-detail">
            <div class="detail-images">
              <img :src="selectedProduct.images.split(',')[0]" :alt="selectedProduct.name" v-if="selectedProduct.images">
              <div v-else class="no-image">无图片</div>
            </div>
            
            <div class="detail-info">
              <h2>{{ selectedProduct.name }}</h2>
              <p class="detail-price">¥{{ selectedProduct.price }}</p>
              <p class="detail-description">{{ selectedProduct.description }}</p>
              <p class="detail-stock">库存: {{ selectedProduct.stock }}</p>
              <div class="detail-status" :class="{ 'on-sale': selectedProduct.status === 1, 'off-sale': selectedProduct.status === 0 }">{{ selectedProduct.status === 1 ? '热卖中' : '已下架' }}</div>
              <button class="buy-btn" v-if="selectedProduct.status === 1">加入购物车</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { productAPI } from '../api/product'

export default {
  name: 'ProductList',
  data() {
    return {
      products: [],
      searchKeyword: '',
      showDetail: false,
      selectedProduct: null
    }
  },
  mounted() {
    this.fetchProducts()
  },
  methods: {
    // 获取商品列表
    fetchProducts() {
      productAPI.getProducts()
        .then(res => {
          this.products = res.data
        })
        .catch(err => {
          console.error('获取商品列表失败:', err.response || err)
          alert('获取商品列表失败: ' + (err.response?.data?.message || err.message || '未知错误'))
        })
    },
    
    // 搜索商品
    searchProducts() {
      productAPI.getProducts({ keyword: this.searchKeyword })
        .then(res => {
          this.products = res.data
        })
        .catch(err => {
          console.error('搜索商品失败:', err)
          alert('搜索商品失败')
        })
    },
    
    // 显示商品详情
    showProductDetail(id) {
      productAPI.getProduct(id)
        .then(res => {
          this.selectedProduct = res.data
          this.showDetail = true
        })
        .catch(err => {
          console.error('获取商品详情失败:', err)
          alert('获取商品详情失败')
        })
    }
  }
}
</script>

<style scoped>
.product-list {
  padding: 20px;
}

.search-bar {
  margin-bottom: 20px;
  text-align: center;
}

.search-bar input {
  padding: 8px;
  width: 300px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.search-bar button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 10px;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.product-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  cursor: pointer;
  transition: transform 0.3s;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.product-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  border-radius: 4px;
}

.no-image {
  width: 100%;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f2f2f2;
  color: #999;
  border-radius: 4px;
}

.product-card h3 {
  margin: 10px 0;
  font-size: 18px;
}

.price {
  color: #e74c3c;
  font-weight: bold;
  font-size: 18px;
  margin: 10px 0;
}

.description {
  color: #666;
  margin-bottom: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.status {
  display: inline-block;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
}

.on-sale {
  background-color: #2ecc71;
  color: white;
}

.off-sale {
  background-color: #95a5a6;
  color: white;
}

.detail-dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-content {
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  width: 800px;
  max-width: 90%;
  position: relative;
}

.close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
}

.product-detail {
  display: flex;
  gap: 20px;
}

.detail-images {
  flex: 1;
}

.detail-images img {
  width: 100%;
  height: 400px;
  object-fit: cover;
  border-radius: 4px;
}

.detail-info {
  flex: 1;
}

.detail-price {
  color: #e74c3c;
  font-weight: bold;
  font-size: 24px;
  margin: 10px 0;
}

.detail-description {
  margin-bottom: 20px;
  line-height: 1.6;
}

.detail-stock {
  margin-bottom: 20px;
}

.detail-status {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 4px;
  margin-bottom: 20px;
}

.buy-btn {
  padding: 10px 20px;
  background-color: #e74c3c;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}
</style>